/*
 * SetupDatabaseComplete.java
 *
 * Created on 29 March 2004, 06:43
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client;

import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import com.velocityme.client.utility.*;
import com.velocityme.session.KeySessionBean;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PermissionDeniedException;
import java.rmi.RemoteException;
import java.util.HashSet;
import java.util.Set;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;

/**
 *
 * @author  Robert
 */
public class SetupDatabaseComplete {
    
    private KeySession m_key;
    
    public SetupDatabaseComplete() {
    }
    
    public boolean login(String password) throws RemoteException, NamingException, CreateException {
        m_key = KeySessionUtil.getHome().create();

        return m_key.login("default", password,getClass().getPackage().getImplementationVersion(), "SetupDatabaseComplete") == KeySessionBean.LOGIN_SUCCESS;
    }
    
    public void setupBugTaskType() throws RemoteException, NamingException, FinderException, CreateException, PermissionDeniedException, InvalidKeyException {
        Integer id = new Integer(0);
        
        NodePK roleNodePK = StatelessRemoteClientSessionUtil.getSession().findSystemNodeByName(m_key, "Setup Roles");
        setupBugRoles(roleNodePK);
        
        NodePK stateNodePK = StatelessRemoteClientSessionUtil.getSession().findSystemNodeByName(m_key, "Setup States");
        setupBugTaskStates(stateNodePK);
        
        NodePK statusNodePK = StatelessRemoteClientSessionUtil.getSession().findSystemNodeByName(m_key, "Setup Statuses");
        setupBugTaskStatuses(statusNodePK);
        
        NodePK taskTypeNodePK = StatelessRemoteClientSessionUtil.getSession().findSystemNodeByName(m_key, "Setup Task Types");
        TaskStateMachineValue bugTaskTypeValue = new TaskStateMachineValue(id);
        bugTaskTypeValue.setNodeValue(new NodeValue(id, "Bug", "Software bug", "TaskStateMachine", "TaskStateMachine"));
        TaskStateMachinePK bugTaskTypePK = StatelessRemoteClientSessionUtil.getSession().taskStateMachineCreate(m_key, taskTypeNodePK, bugTaskTypeValue);
        
        setupbugTaskTransitions(bugTaskTypePK);
    }
    
    RoleValue m_analystRoleValue = null;
    RoleValue m_designerRoleValue = null;
    RoleValue m_reviewerRoleValue = null;
    RoleValue m_implementerRoleValue = null;
    RoleValue m_testerRoleValue = null;
    
    private void setupBugRoles(NodePK p_roleNodePK) throws NamingException {
        Integer id = new Integer(0);
        Set permissionStrings = new HashSet();
        
        m_analystRoleValue = new RoleValue(id, Boolean.TRUE);
        m_analystRoleValue.setNodeValue(new NodeValue(id, "Analyst", "", "Role", "Role"));
    }
    
    StateValue toBeAnalysedStateValue = null;
    StateValue analysingStateValue = null;
    StateValue toBeDesignedStateValue = null;
    StateValue designingStateValue = null;
    StateValue toBeReviewedStateValue = null;
    StateValue reviewingStateValue = null;
    StateValue toBeImplementedStateValue = null;
    StateValue implementingStateValue = null;
    StateValue toBeTestedStateValue = null;
    StateValue testingStateValue = null;
    
    private void setupBugTaskStates(NodePK p_stateNodePK) throws RemoteException, NamingException, CreateException, PermissionDeniedException, InvalidKeyException {
        Integer id = new Integer(0);
        
        toBeAnalysedStateValue = new StateValue(id, Boolean.FALSE);
        toBeAnalysedStateValue.setNodeValue(new NodeValue(id, "To Be Analysed", "", "State", "State"));
        StatelessRemoteClientSessionUtil.getSession().stateCreate(m_key, p_stateNodePK, toBeAnalysedStateValue);

        analysingStateValue = new StateValue(id, Boolean.TRUE);
        analysingStateValue.setNodeValue(new NodeValue(id, "Analysing", "", "State", "State"));
        StatelessRemoteClientSessionUtil.getSession().stateCreate(m_key, p_stateNodePK, analysingStateValue);

        toBeDesignedStateValue = new StateValue(id, Boolean.FALSE);
        toBeDesignedStateValue.setNodeValue(new NodeValue(id, "To Be Designed", "", "State", "State"));
        StatelessRemoteClientSessionUtil.getSession().stateCreate(m_key, p_stateNodePK, toBeDesignedStateValue);

        designingStateValue = new StateValue(id, Boolean.TRUE);
        designingStateValue.setNodeValue(new NodeValue(id, "Designing", "", "State", "State"));
        StatelessRemoteClientSessionUtil.getSession().stateCreate(m_key, p_stateNodePK, designingStateValue);

        toBeReviewedStateValue = new StateValue(id, Boolean.FALSE);
        toBeReviewedStateValue.setNodeValue(new NodeValue(id, "To Be Reviewed", "", "State", "State"));
        StatelessRemoteClientSessionUtil.getSession().stateCreate(m_key, p_stateNodePK, toBeReviewedStateValue);

        reviewingStateValue = new StateValue(id, Boolean.TRUE);
        reviewingStateValue.setNodeValue(new NodeValue(id, "Reviewing", "", "State", "State"));
        StatelessRemoteClientSessionUtil.getSession().stateCreate(m_key, p_stateNodePK, reviewingStateValue);

        toBeImplementedStateValue = new StateValue(id, Boolean.FALSE);
        toBeImplementedStateValue.setNodeValue(new NodeValue(id, "To Be Implemented", "", "State", "State"));
        StatelessRemoteClientSessionUtil.getSession().stateCreate(m_key, p_stateNodePK, toBeImplementedStateValue);

        implementingStateValue = new StateValue(id, Boolean.TRUE);
        implementingStateValue.setNodeValue(new NodeValue(id, "Implementing", "", "State", "State"));
        StatelessRemoteClientSessionUtil.getSession().stateCreate(m_key, p_stateNodePK, implementingStateValue);

        toBeTestedStateValue = new StateValue(id, Boolean.FALSE);
        toBeTestedStateValue.setNodeValue(new NodeValue(id, "To Be Tested", "", "State", "State"));
        StatelessRemoteClientSessionUtil.getSession().stateCreate(m_key, p_stateNodePK, toBeTestedStateValue);

        testingStateValue = new StateValue(id, Boolean.TRUE);
        testingStateValue.setNodeValue(new NodeValue(id, "Testing", "", "State", "State"));
        StatelessRemoteClientSessionUtil.getSession().stateCreate(m_key, p_stateNodePK, testingStateValue);   
    }
    
    StatusValue reportedStatusValue = null;
    StatusValue startedAnalysingStatusValue = null;
    StatusValue rejectedByAnalysistStatusValue = null;
    StatusValue analysedStatusValue = null;
    StatusValue startedDesigningStatusValue = null;
    StatusValue rejectedByDesignerStatusValue = null;
    StatusValue designedStatusValue = null;
    StatusValue startedReviewingStatusValue = null;
    StatusValue rejectedByReviewerStatusValue = null;
    StatusValue reviewedStatusValue = null;
    StatusValue startedImplementingStatusValue = null;
    StatusValue designIncompatableStatusValue = null;
    StatusValue analysisIncompatableStatusValue = null;
    StatusValue implementedStatusValue = null;
    StatusValue startedTestingStatusValue = null;
    StatusValue testingFailedStatusValue = null;
    StatusValue testedStatusValue = null;

    private void setupBugTaskStatuses(NodePK p_statusNodePK) throws RemoteException, NamingException, CreateException, PermissionDeniedException, InvalidKeyException {
        Integer id = new Integer(0);
        
        reportedStatusValue = new StatusValue(id);
        reportedStatusValue.setNodeValue(new NodeValue(id, "Reported", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, reportedStatusValue);
        
        startedAnalysingStatusValue = new StatusValue(id);
        startedAnalysingStatusValue.setNodeValue(new NodeValue(id, "Started Analysing", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, startedAnalysingStatusValue);
        
        rejectedByAnalysistStatusValue = new StatusValue(id);
        rejectedByAnalysistStatusValue.setNodeValue(new NodeValue(id, "Rejected By Analyst", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, rejectedByAnalysistStatusValue);
        
        analysedStatusValue = new StatusValue(id);
        analysedStatusValue.setNodeValue(new NodeValue(id, "Analysed", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, analysedStatusValue);
        
        startedDesigningStatusValue = new StatusValue(id);
        startedDesigningStatusValue.setNodeValue(new NodeValue(id, "Started Designing", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, startedDesigningStatusValue);
        
        rejectedByDesignerStatusValue = new StatusValue(id);
        rejectedByDesignerStatusValue.setNodeValue(new NodeValue(id, "Rejected By Designer", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, rejectedByDesignerStatusValue);
        
        designedStatusValue = new StatusValue(id);
        designedStatusValue.setNodeValue(new NodeValue(id, "Designed", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, designedStatusValue);
        
        startedReviewingStatusValue = new StatusValue(id);
        startedReviewingStatusValue.setNodeValue(new NodeValue(id, "Started Reviewing", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, startedReviewingStatusValue);
        
        rejectedByReviewerStatusValue = new StatusValue(id);
        rejectedByReviewerStatusValue.setNodeValue(new NodeValue(id, "Rejected By Reviewer", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, rejectedByReviewerStatusValue);
        
        reviewedStatusValue = new StatusValue(id);
        reviewedStatusValue.setNodeValue(new NodeValue(id, "Reviewed", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, reviewedStatusValue);
        
        startedImplementingStatusValue = new StatusValue(id);
        startedImplementingStatusValue.setNodeValue(new NodeValue(id, "Started Implementing", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, startedImplementingStatusValue);
        
        designIncompatableStatusValue = new StatusValue(id);
        designIncompatableStatusValue.setNodeValue(new NodeValue(id, "Design Incompatable", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, designIncompatableStatusValue);
        
        analysisIncompatableStatusValue = new StatusValue(id);
        analysisIncompatableStatusValue.setNodeValue(new NodeValue(id, "Analysis Incompatable", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, analysisIncompatableStatusValue);
        
        implementedStatusValue = new StatusValue(id);
        implementedStatusValue.setNodeValue(new NodeValue(id, "Implemented", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, implementedStatusValue);
        
        startedTestingStatusValue = new StatusValue(id);
        startedTestingStatusValue.setNodeValue(new NodeValue(id, "Started Testing", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, startedTestingStatusValue);
        
        testingFailedStatusValue = new StatusValue(id);
        testingFailedStatusValue.setNodeValue(new NodeValue(id, "Testing Failed", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, testingFailedStatusValue);
        
        testedStatusValue = new StatusValue(id);
        testedStatusValue.setNodeValue(new NodeValue(id, "Tested", "", "Status", "Status"));
        StatelessRemoteClientSessionUtil.getSession().statusCreate(m_key, p_statusNodePK, testedStatusValue);
    }
    
    private void setupbugTaskTransitions(TaskStateMachinePK bugTaskTypePK) {
        Integer id = new Integer(0);
        
        TransitionValue reportedTransition = new TransitionValue(id, Boolean.FALSE, Boolean.FALSE);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String password = new String("fred");
        try {
            SetupDatabaseComplete setup = new SetupDatabaseComplete();
            setup.login(password);
            setup.setupBugTaskType();
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    
}
